/**
 * @File SegmentSummaryController.java
 * @author Dheeraj Singh
 * @Version 1.0
 * @Date June 22, 2013
 * 
 * Copyright (c) 2011 EdRP, Dayalbagh Educational Institute.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this  list of conditions and the following disclaimer.
 *
 * Redistribution in binary form must reproducuce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL ETRG OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributors: Members of EdRP, Dayalbagh Educational Institute
 */

package in.ac.dei.edrp.generalLedger.flexFieldSettings.segmentSummary;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.stereotype.Controller;

import flex.messaging.FlexContext;
import flex.messaging.FlexSession;

@Controller
@RemotingDestination("segmentSummary")
public class SegmentSummaryController {

	@Autowired
	@Qualifier("SegmentSummaryDaoImpl")
	private SegmentSummaryDao segmentSummaryDao;
	
	public List<SegmentSummaryBean> getAllSegments(String applicationId, String flexFieldId, String flexFieldCode){
		List<SegmentSummaryBean> list = null;
		FlexSession session = FlexContext.getFlexSession();
		SegmentSummaryBean segmentSummaryBean = new SegmentSummaryBean();
		segmentSummaryBean.setApplicationId(applicationId);
		segmentSummaryBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentSummaryBean.setFlexFieldId(flexFieldId);
		segmentSummaryBean.setFlexFieldCode(flexFieldCode);
		list = segmentSummaryDao.getAllSegments(segmentSummaryBean);
		return list;
	}
	
	public List<SegmentSummaryBean> getFindSegments(String applicationId, String flexFieldId, String flexFieldCode){
		List<SegmentSummaryBean> list = null;
		FlexSession session = FlexContext.getFlexSession();
		SegmentSummaryBean segmentSummaryBean = new SegmentSummaryBean();
		segmentSummaryBean.setApplicationId(applicationId);
		segmentSummaryBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentSummaryBean.setFlexFieldId(flexFieldId);
		segmentSummaryBean.setFlexFieldCode(flexFieldCode);
		list = segmentSummaryDao.getFindSegments(segmentSummaryBean);
		return list;
	}
	
	public List<SegmentSummaryBean> getFindColumns(){
		List<SegmentSummaryBean> list = null;
		FlexSession session = FlexContext.getFlexSession();
		SegmentSummaryBean segmentSummaryBean = new SegmentSummaryBean();
		segmentSummaryBean.setApplicationId(session.getAttribute("applicationId").toString());
		segmentSummaryBean.setOrganizationId(session.getAttribute("universityCode").toString());
		list = segmentSummaryDao.getFindColumns(segmentSummaryBean);
		return list;
	}
	
	public List<SegmentSummaryBean> getFindValueSets(String applicationId, String flexFieldId, String flexFieldCode){
		List<SegmentSummaryBean> list = null;
		FlexSession session = FlexContext.getFlexSession();
		SegmentSummaryBean segmentSummaryBean = new SegmentSummaryBean();
		segmentSummaryBean.setApplicationId(applicationId);
		segmentSummaryBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentSummaryBean.setFlexFieldId(flexFieldId);
		segmentSummaryBean.setFlexFieldCode(flexFieldCode);
		list = segmentSummaryDao.getFindValueSets(segmentSummaryBean);
		return list;
	}
	
	public boolean setSegment(Map<String, String> obj){
		boolean flag = false;
		FlexSession session = FlexContext.getFlexSession();
		SegmentSummaryBean segmentSummaryBean = new SegmentSummaryBean();
		segmentSummaryBean.setApplicationId(obj.get("applicationId"));
		segmentSummaryBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentSummaryBean.setFlexFieldId(obj.get("flexFieldId"));
		segmentSummaryBean.setFlexFieldCode(obj.get("flexFieldCode"));
		segmentSummaryBean.setSegmentName(obj.get("segmentName"));
		segmentSummaryBean.setSegmentDescription(obj.get("segmentDescription"));
		segmentSummaryBean.setSegmentColumnCode(obj.get("segmentColumnCode"));
		segmentSummaryBean.setValueSetId(obj.get("valueSetId"));
		segmentSummaryBean.setSequenceNumber(Integer.parseInt(obj.get("sequenceNumber")));
		segmentSummaryBean.setCreatedBy(session.getAttribute("creatorId").toString());
		flag = segmentSummaryDao.setSegment(segmentSummaryBean);
		return flag;
	}
	
	public String checkSequence(String applicationId, String flexFieldId, int sequenceNumber, int segmentId, String flexFieldCode){
		String str = "";
		FlexSession session = FlexContext.getFlexSession();
		SegmentSummaryBean segmentSummaryBean = new SegmentSummaryBean();
		segmentSummaryBean.setApplicationId(applicationId);
		segmentSummaryBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentSummaryBean.setFlexFieldId(flexFieldId);
		segmentSummaryBean.setFlexFieldCode(flexFieldCode);
		segmentSummaryBean.setSequenceNumber(sequenceNumber);
		segmentSummaryBean.setSegmentId(segmentId);
		str = segmentSummaryDao.checkSequence(segmentSummaryBean);
		return str;
	}
	
	public String checkSegmentColumn(String applicationId, String flexFieldId, String segmentColumn, int segmentId, String flexFieldCode){
		String str = "";
		FlexSession session = FlexContext.getFlexSession();
		SegmentSummaryBean segmentSummaryBean = new SegmentSummaryBean();
		segmentSummaryBean.setApplicationId(applicationId);
		segmentSummaryBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentSummaryBean.setFlexFieldId(flexFieldId);
		segmentSummaryBean.setFlexFieldCode(flexFieldCode);
		segmentSummaryBean.setSegmentColumnCode(segmentColumn);
		segmentSummaryBean.setSegmentId(segmentId);
		str = segmentSummaryDao.checkSegmentColumn(segmentSummaryBean);
		return str;
	}
	
	public List<SegmentSummaryBean> findSegment(String applicationId, String flexFieldId, int segmentId, String flexFieldCode){
		List<SegmentSummaryBean> list = null;
		FlexSession session = FlexContext.getFlexSession();
		SegmentSummaryBean segmentSummaryBean = new SegmentSummaryBean();
		segmentSummaryBean.setApplicationId(applicationId);
		segmentSummaryBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentSummaryBean.setFlexFieldId(flexFieldId);
		segmentSummaryBean.setFlexFieldCode(flexFieldCode);
		segmentSummaryBean.setSegmentId(segmentId);
		list = segmentSummaryDao.findSegment(segmentSummaryBean);
		return list;
	}
	
	public boolean updateSegment(Map<String, String> obj){
		boolean flag = false;
		FlexSession session = FlexContext.getFlexSession();
		SegmentSummaryBean segmentSummaryBean = new SegmentSummaryBean();
		segmentSummaryBean.setApplicationId(obj.get("applicationId"));
		segmentSummaryBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentSummaryBean.setFlexFieldId(obj.get("flexFieldId"));
		segmentSummaryBean.setFlexFieldCode(obj.get("flexFieldCode"));
		segmentSummaryBean.setSegmentId(Integer.parseInt(obj.get("segmentId")));
		segmentSummaryBean.setSegmentName(obj.get("segmentName"));
		segmentSummaryBean.setSegmentDescription(obj.get("segmentDescription"));
		segmentSummaryBean.setSegmentColumnCode(obj.get("segmentColumnCode"));
		segmentSummaryBean.setValueSetId(obj.get("valueSetId"));
		segmentSummaryBean.setSequenceNumber(Integer.parseInt(obj.get("sequenceNumber")));
		segmentSummaryBean.setLastUpdatedBy(session.getAttribute("modifierId").toString());
		flag = segmentSummaryDao.updateSegment(segmentSummaryBean);
		return flag;
	}
	
	public List<SegmentSummaryBean> getQualifiers(String applicationId, String flexFieldId){
		List<SegmentSummaryBean> list = null;
		FlexSession session = FlexContext.getFlexSession();
		SegmentSummaryBean segmentSummaryBean = new SegmentSummaryBean();
		segmentSummaryBean.setApplicationId(applicationId);
		segmentSummaryBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentSummaryBean.setFlexFieldId(flexFieldId);
		list = segmentSummaryDao.getQualifiers(segmentSummaryBean);
		return list;
	}
	
	public boolean setQualifiers(Map<String, String> obj){
		boolean flag = false;
		FlexSession session = FlexContext.getFlexSession();
		SegmentSummaryBean segmentSummaryBean = new SegmentSummaryBean();
		segmentSummaryBean.setApplicationId(obj.get("applicationId"));
		segmentSummaryBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentSummaryBean.setFlexFieldId(obj.get("flexFieldId"));
		segmentSummaryBean.setFlexFieldCode(obj.get("flexFieldCode"));
		segmentSummaryBean.setSegmentId(Integer.parseInt(obj.get("segmentId")));
		segmentSummaryBean.setQualifierId(obj.get("qualifierId"));
		segmentSummaryBean.setLastUpdatedBy(session.getAttribute("modifierId").toString());
		flag = segmentSummaryDao.setQualifiers(segmentSummaryBean);
		return flag;
	}
}
